//
//  TestViewController4.swift
//  demo01
//
//  Created by babybus on 2025/6/27.
//

import Foundation
import UIKit
class NestedCollection:UIViewController,
                          UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate{
    
    private var collectionView:UICollectionView!
    private var items:[String]=[
        "首页模块","走访任务管理","集团业务嵌入式审批","终端管理"
    ]
    private var ModuleInfoItems:[[ModuleInfo]]=[
        [ModuleInfo(moduleName: "生产管理",moduleImg: "./11.png"),
        ModuleInfo(moduleName: "集中调度", moduleImg: "./11.png"),
         ModuleInfo(moduleName: "订单管理",moduleImg: "./11.png"),
         ModuleInfo(moduleName: "千机变现",moduleImg: "./11.png"),
         ModuleInfo(moduleName: "业务展示",moduleImg: "./11.png"),
         ModuleInfo(moduleName: "流程引擎",moduleImg: "./11.png"),
         ModuleInfo(moduleName: "即客H5", moduleImg: "./11.png")],
        
        [ModuleInfo(moduleName: "制定走访", moduleImg:"./22.png"),
         ModuleInfo(moduleName: "走访任务", moduleImg:"./22.png"),
         ModuleInfo(moduleName: "客户催缴", moduleImg:"./22.png")],
        
        [ModuleInfo(moduleName: "开具发票", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "台账登记", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "特号办理", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "营销方案", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "基础业务", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "派车功能", moduleImg:"./33.png"),
          ModuleInfo(moduleName: "全省终端", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "分公司项目", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "分公司资产", moduleImg:"./33.png"),
         ModuleInfo(moduleName: "投诉审核", moduleImg:"./33.png"),
          ModuleInfo(moduleName: "出行权益", moduleImg:"./33.png")],
        
        [ModuleInfo(moduleName: "终端订购", moduleImg:"./44.png"),
         ModuleInfo(moduleName: "终端退库", moduleImg:"./44.png"),
         ModuleInfo(moduleName: "终端售后", moduleImg:"./44.png")]
    ]
    
    override func viewDidLoad(){
        super.viewDidLoad()
        self.navigationController?.navigationBar.backgroundColor = .white;
        self.navigationItem.title = "全部模块";
        let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.left")?.withTintColor(.gray,renderingMode: .alwaysOriginal),
                                            style: .plain,
                                            target: self,
                                            action: #selector(backAction))
        navigationItem.leftBarButtonItem = backButton;
        
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .vertical;
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(ModuleCell.self, forCellWithReuseIdentifier: "ModuleCell")
        collectionView.register(MyHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "MyHeader")
        
        self.view.addSubview(collectionView)
        
        collectionView.backgroundColor = .systemGray6
        collectionView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalToSuperview()
        }
    }
    @objc private func backAction() {
        navigationController?.popViewController(animated: true)
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ModuleCell", for: indexPath)as! ModuleCell
        let labelName = items[indexPath.row]
        cell.cellLabel.text = labelName
        
        let moduleinfo = ModuleInfoItems[indexPath.row]
        cell.configure(item: moduleinfo)
        
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "MyHeader", for: indexPath)as! MyHeaderView
            return header
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.bounds.width-20)
        let allOfItems = ModuleInfoItems[indexPath.row].count
        let numsOfRows = ceil(CGFloat(allOfItems)/4)
        
        return CGSize(width: width, height: numsOfRows*100+80);
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 0)
    }
    
}
